package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

public class ManageServlet extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //我们只需 要在doGet中获取到请求的参数q=list，然后根据获取到的参数来决定调用哪一个方法
    String method = req.getParameter("q");
    if("delete".equals(method)) {
      delete(req,resp);
    }else if("update".equals(method)){
      update(req,resp);
    }else if("add".equals(method)) {
      add(req, resp);
    }else {
      list(req,resp);
    }
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doGet(req,resp);
  }

  //查询所有信息的方法
  protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html;charset=utf-8");
    HttpSession session  = req.getSession();
    PrintWriter out = resp.getWriter();
    //调用业务层查询方法
    GuestbookService service = new GuestbookServiceImpl();
    List<Object> list = service.queryInfo();
    //将查询到的list集合保存到服务器端
    session.setAttribute("list",list);
    //之后就可以将这个请求转发给list.jsp
    //    req.getRequestDispatcher("/list.jsp").forward(req,resp);
    resp.sendRedirect(req.getContextPath()+"/list.jsp");
  }

  protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html;charset=utf-8");
    String name = req.getParameter("name");
    String phone = req.getParameter("phone");
    String email = req.getParameter("email");
    String title = req.getParameter("title");
    String content = req.getParameter("content");
    String createdtime = req.getParameter("createdtime");
    Guestbook guestbook=new Guestbook();
    guestbook.setName(name);
    guestbook.setPhone(phone);
    guestbook.setEmail(email);
    guestbook.setTitle(title);
    guestbook.setContent(content);
    guestbook.setCreatedtime(createdtime);
    GuestbookService service = new GuestbookServiceImpl();
    boolean isright=service.add(guestbook);
    System.out.println(isright==true ?"添加成功":"添加失败");
    list(req,resp);

  }

  //删除
  protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html;charset=utf-8");
    GuestbookService service = new GuestbookServiceImpl();
    String id = req.getParameter("id");
    int del_id = Integer.parseInt(id);
    boolean isRight = service.del(del_id);
    if(isRight){
      req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
    }else {
      System.out.println("error......");
    }
  }

  //修改
  protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

  }

}
